/*
 * 
 * Version 1.00
 * 
 * Created on 2007-6-4
 * Encoding GB2312
 */
package net.comm.group.service.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import java.util.Vector;

import net.comm.check.service.impl.JifenServiceImpl;
import net.comm.check.web.form.JifenForm;
import net.comm.common.Email;
import net.comm.common.GlbFinal;
import net.comm.common.ReturnMessage;
import net.comm.param.service.impl.SequenceMgr;
import net.comm.common.common;


import net.comm.file.service.impl.FilesServiceImpl;
import net.comm.file.web.form.FilesForm;
import net.comm.group.bean.BaseGroups;
import net.comm.group.db.DbGroupGroupByFirsortService;
import net.comm.group.db.DbGroupsService;
import net.comm.group.db.GMemberServiceSqlMysql;
import net.comm.group.db.GroupsServiceSqlMysql;
import net.comm.group.web.form.GroupsForm;
import net.comm.page.PageCtr;

import net.comm.tags.JstlFun;
import net.comm.user.service.impl.UserSysinfoServiceImpl;
import net.comm.user.web.form.UserSysinfoForm;
import net.comm.util.DatetimeUtil;
import net.comm.util.DbcpPool;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.sakaiproject.db.impl.BasicSqlService;


/**
 *
 * @author hcgong(hcgong@sina.com)
 * @version 1.0
 * @date 2007-6-4
 */
public class GroupsServiceImpl {

	static Logger logger = Logger.getLogger(GroupsServiceImpl.class.getName());

	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List list(GroupsForm theForm,  ReturnMessage msg) 
	{
		List list = null;

		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			GroupsServiceSqlMysql servSql = new GroupsServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			/*
			 * 获得查询SQL语句
			 */
			String sql = "";
			PageCtr page = theForm.getPage();
			if(theForm.getQryUserID() == 0){
				
				sql = servSql.getCountSql1();
				/**
				 * 根据SQL执行结果计算分页相关信息
				 */
				page.setTotalNum(bSqlServ.dbCount(cn, sql, null));
				page.caculate();
			}else{

				Object[] fields1 = new Object[1];
				
				fields1[0] = theForm.getQryUserID();
				
				sql = servSql.getCountSql3();
				/**
				 * 根据SQL执行结果计算分页相关信息
				 */
				page.setTotalNum(bSqlServ.dbCount(cn, sql, fields1));
				page.caculate();	
			}
			
//			if(theForm.getIsradnom().equals(GlbFinal.YES)){
//
//				int pos = (int)(Math.random()*(page.getTotalNum()- page.getPageSize()));
//				if(pos > 0)
//					page.setPos(pos);
//			}				
			
			if(theForm.getQryUserID() == 0 ){
			
				//取得FORM中传入的查询参数
				Object[] fields = new Object[2];
				
				fields[0] = page.getPos()-1;
				fields[1] = page.getPageSize();
				sql = servSql.getRecbyPageSql();
				list = bSqlServ.dbRead(cn, sql, fields, new DbGroupsService());
			}else{
				//查询好友创建的群组
				Object[] fields = new Object[3];
				
				fields[0] = theForm.getQryUserID();
				fields[1] = page.getPos()-1;
				fields[2] = page.getPageSize();
				sql = servSql.getRecbyFruserIDSql();

				list = bSqlServ.dbRead(cn, sql, fields, new DbGroupsService());
			}
			
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}		
		
		return list;
	}

	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List adlist(GroupsForm theForm,  ReturnMessage msg) 
	{
		List list = new Vector();

		Connection cn = null;
		Statement stmt=null;
		ResultSet rs=null;
		try
		{
			cn = DbcpPool.getConnection();
			
			GroupsServiceSqlMysql servSql = new GroupsServiceSqlMysql();
			
			StringBuffer sb = new StringBuffer(" where 1=1 ");
			
			String qryName = theForm.getQryName().replaceAll(GlbFinal.SQLINjection, " ").trim();
			if(!qryName.equals("")){
				
				sb.append(" and name like '%");
				sb.append(qryName);
				sb.append("%' ");
			}

			int qryUserID = theForm.getQryUserID();
			if(qryUserID > 0 ){
				
				sb.append(" and userID = ");
				sb.append(qryUserID);
				sb.append(" ");
			}
			if(theForm.getQryFirs() != 0){
				
				sb.append(" and Firsort =");
				sb.append(theForm.getQryFirs());
				sb.append(" ");
			}
			if(theForm.getQrySecs() != 0){
				
				sb.append(" and secsort =");
				sb.append(theForm.getQrySecs());
				sb.append(" ");
			}
			if(theForm.getQryThis() != 0){
				
				sb.append(" and ThiSort =");
				sb.append(theForm.getQryThis());
				sb.append(" ");
			}			
			if(theForm.getQryPri() != 0){
				
				sb.append(" and Work_location =");
				sb.append(theForm.getQryPri());
				sb.append(" ");
			}
			if(theForm.getQryCity() != 0){
				
				sb.append(" and Work_sublocation =");
				sb.append(theForm.getQryCity());
				sb.append(" ");
			}
			if(theForm.getQryVillageID() != 0){
				
				sb.append(" and VillageID =");
				sb.append(theForm.getQryVillageID());
				sb.append(" ");
			}
			
			String qryStatus = theForm.getQryStatus().replaceAll(GlbFinal.SQLINjection, "");
			if(!qryStatus.equals("")){

				sb.append(" and status ='");
				sb.append(qryStatus);
				sb.append("' ");
			}
			
			String qryOrderby = theForm.getQryOrderby().replaceAll(GlbFinal.SQLINjection, " ").trim();
			if(!qryOrderby.equals("")){
				
				sb.append(" Order by ");
				sb.append(qryOrderby);
				sb.append(" ");
			}else{
				sb.append(" Order by Ctime desc ");
			}

			//查询记录数
			stmt = cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			String sql = servSql.getCountSql1();

			logger.debug(sql+sb.toString());
			rs=stmt.executeQuery(sql+sb.toString());
			int nRows = 0;
			if (rs.next())
	           nRows = rs.getInt("COUNT");
			else
			{
	            rs.close();
	            stmt.close();
	            DbcpPool.returnConnection(cn);
			    return list;
			}
			/**
			 * 根据SQL执行结果计算分页相关信息
			 */
			PageCtr page = theForm.getPage();
			page.setTotalNum(nRows);
			page.caculate();
		
			if(theForm.getIsradnom().equals(GlbFinal.YES)){

				int pos = (int)(Math.random()*(page.getTotalNum()-page.getPageSize()));
				if(pos < 1)
					pos = 1;
				page.setPos(pos);
			}			

			sb.append(" limit ");
			sb.append(page.getPos()-1);
			sb.append(", ");
			sb.append(page.getPageSize());
			
			 //取记录
			 sql = servSql.getRecSql();
			 logger.debug(sql+sb.toString());
			 DbGroupsService dbservice = new DbGroupsService();
			 
			 rs=stmt.executeQuery(sql+sb.toString());
			 while (rs.next())
			 {
				try
				{
					Object obj = dbservice.readSqlResultRecord(rs);
					if (obj != null) list.add(obj);
				}
				catch (Throwable t)
				{
					logger.error(t.getMessage());
				}		
			 }
			 rs.close();
			 stmt.close();
		     DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try {
				if (rs != null)
					rs.close();
				if (stmt != null)
					stmt.close();
				DbcpPool.returnConnection(cn);
			} catch (Exception ex1) {
				ex1.printStackTrace();
			}			
	    }finally {
			try {
				if (rs != null)
					rs.close();
				if (stmt != null)
					stmt.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}			
			DbcpPool.returnConnection(cn);
		}		
		return list;
	}
	/**
	 * 返回根据会员ID查询的群组
	 */
	public List list(String  userID) 
	{
		List list = null;
		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			
			fields[0] = userID;			

			GroupsServiceSqlMysql servSql = new GroupsServiceSqlMysql();
						
			BasicSqlService  bSqlServ = new BasicSqlService();

			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getByUserIDSql();
									
			list = bSqlServ.dbRead(cn, sql, fields, new DbGroupsService());
									
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return list;
	}
	/**
	 * 返回根据First查询的群组
	 */
	public List listbyFirSecsort(GroupsForm theForm,  ReturnMessage msg) 
	{
		List list = null;
		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			GroupsServiceSqlMysql servSql = new GroupsServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			//取得FORM中传入的查询参数
			Object[] fields = new Object[3];
			
			fields[0] = theForm.getQryVillageID();			
			fields[1] = theForm.getQryFirs();			
			fields[2] = theForm.getPage().getPageSize();			

			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getGroupByFirSecsortSql();
									
			list = bSqlServ.dbRead(cn, sql, fields, new DbGroupsService());
									
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return list;
	}	
	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List listmyadd(GroupsForm theForm,  ReturnMessage msg) 
	{
		List list = null;

		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			GroupsServiceSqlMysql servSql = new GroupsServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			/*
			 * 获得查询SQL语句
			 */
			String sql = "";
			PageCtr page = theForm.getPage();
			Object[] fields1 = new Object[3];
			
			fields1[0] = theForm.getQryUserID();
			fields1[1] = theForm.getQryAddType();
			if(theForm.getQryAddType().equals("1")){
				
				fields1[2] = "2";
			}
			
			sql = servSql.getCountSql4();
			/**
			 * 根据SQL执行结果计算分页相关信息
			 */
			page.setTotalNum(bSqlServ.dbCount(cn, sql, fields1));
			page.caculate();	
	
			//查询好友创建的群组
			Object[] fields = new Object[5];
			
			fields[0] = theForm.getQryUserID();
			fields[1] = theForm.getQryAddType();
			if(theForm.getQryAddType().equals("1")){
				
				fields1[2] = "2";
			}
			fields[3] = page.getPos()-1;
			fields[4] = page.getPageSize();
			
			sql = servSql.getRecmyAddSql();

			list = bSqlServ.dbRead(cn, sql, fields, new DbGroupsService());			
			
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}		
		
		return list;
	}
//
//	/**
//	 * 一周热门群组
//	 */
//	public List weekhotlist(int num) 
//	{
//		List list = null;
//		Connection cn = null;
//
//		try
//		{
//			cn = DbcpPool.getConnection();
//			
//			//取得FORM中传入的查询参数
//			Object[] fields = new Object[1];
//			
//			fields[0] = num;			
//
//			GroupsServiceSqlMysql servSql = new GroupsServiceSqlMysql();
//						
//			BasicSqlService  bSqlServ = new BasicSqlService();
//
//			/*
//			 * 获得查询SQL语句
//			 */
//			String sql = servSql.getRecbyOrderbyPepoleValSql();
//									
//			list = bSqlServ.dbRead(cn, sql, fields, new DbGroupsService());
//									
//		    DbcpPool.returnConnection(cn);
//		    
//		} catch(Exception ex) 
//		{
//			/**
//			 * 记录相关日志
//			 */
//			logger.error(ex.getMessage());
//			try 
//			{
//				DbcpPool.rollBackTran(cn);
//				DbcpPool.closeTran(cn);
//				DbcpPool.returnConnection(cn);
//			} catch(Exception ex1) 
//			{
//				ex1.printStackTrace();
//				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
//			}			
//			
//		} finally {
//			DbcpPool.returnConnection(cn);
//		}
//		
//		return list;
//	}
//	/**
//	 * 新成立群组
//	 */
//	public List newclist(int num) 
//	{
//		List list = null;
//		Connection cn = null;
//
//		try
//		{
//			cn = DbcpPool.getConnection();
//			
//			//取得FORM中传入的查询参数
//			Object[] fields = new Object[1];
//			
//			fields[0] = num;			
//
//			GroupsServiceSqlMysql servSql = new GroupsServiceSqlMysql();
//						
//			BasicSqlService  bSqlServ = new BasicSqlService();
//
//			/*
//			 * 获得查询SQL语句
//			 */
//			String sql = servSql.getRecbyOrderbyCTimeSql();
//									
//			list = bSqlServ.dbRead(cn, sql, fields, new DbGroupsService());
//									
//		    DbcpPool.returnConnection(cn);
//		    
//		} catch(Exception ex) 
//		{
//			/**
//			 * 记录相关日志
//			 */
//			logger.error(ex.getMessage());
//			try 
//			{
//				DbcpPool.rollBackTran(cn);
//				DbcpPool.closeTran(cn);
//				DbcpPool.returnConnection(cn);
//			} catch(Exception ex1) 
//			{
//				ex1.printStackTrace();
//				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
//			}			
//			
//		} finally {
//			DbcpPool.returnConnection(cn);
//		}
//		
//		return list;
//	}
	/**
	 * 新成立群组
	 */
	public List listGroupByFirsort() 
	{
		List list = null;
		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			

			GroupsServiceSqlMysql servSql = new GroupsServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getRecGroupbyFirsortSql();
									
			list = bSqlServ.dbRead(cn, sql, null, new DbGroupGroupByFirsortService());
									
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return list;
	}	
	/**
	 * 取推荐群组，groupID为空，则随机取群组，不为空，则取该群组同城市的群组
	 */
	public List TjGroups( int groupID) 
	{
		List list = null;
		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			GroupsServiceSqlMysql servSql = new GroupsServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			String sql = "";
			Object[] fields = new Object[2];
			
			if(groupID == 0){
				sql = servSql.getCountSql1();
				int num = bSqlServ.dbCount(cn, sql, null);

				fields[0] = (int)(Math.random()*(num - 4));			
				fields[1] = 4;			
				
				sql = servSql.getRecbyPageSql();
			}else{
				
				fields[0] = groupID;			
				fields[1] = 4;		
				
				sql = servSql.getByCitySql();
			}
			list = bSqlServ.dbRead(cn, sql, fields, new DbGroupsService());
									
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return list;
	}
	
	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public void get(GroupsForm theForm, ReturnMessage msg) 
	{
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			List list = null;

			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			
			fields[0] = theForm.getGroupID();			
			
			GroupsServiceSqlMysql servSql = new GroupsServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getbykeySql();			
					
			list = bSqlServ.dbRead(cn, sql, fields, new DbGroupsService());
			if(list.size() ==0){
				common.setAppFailMsg(msg , "取群组信息失败！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			BaseGroups base = (BaseGroups)list.get(0);
				
			//更新人气值
			DbcpPool.beginTran(cn);
			
			theForm.Clear();
			theForm.setGroupID(base.getGroupID());
			theForm.setPepoleVal(1);
			
			updNum(cn, theForm, msg); 			

			BeanUtils.copyProperties(theForm, base);
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public void getgroup(GroupsForm theForm, ReturnMessage msg) 
	{
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			List list = null;

			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			
			fields[0] = theForm.getGroupID();			
			
			GroupsServiceSqlMysql servSql = new GroupsServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getbykeySql();			
					
			list = bSqlServ.dbRead(cn, sql, fields, new DbGroupsService());
			if(list.size() ==0){
				common.setAppFailMsg(msg , "取群组信息失败！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			BaseGroups base = (BaseGroups)list.get(0);
				
			BeanUtils.copyProperties(theForm, base);
			
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}	
	/**
	 * 添加群组到表中
	 * @see net.IUserbasicService.module.other.IUserService#add(net.swift.common.ReturnMessage)
	 */
	public void add(GroupsForm theForm,String path, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			//判断群组名称是否已经使用
			List list = null;

			Object[] fields1 = new Object[2];
			
			fields1[0] = theForm.getName();			
			fields1[1] = theForm.getVillageID();			
			
			GroupsServiceSqlMysql servSql = new GroupsServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getbyNameSql();			
					
			list = bSqlServ.dbRead(cn, sql, fields1, new DbGroupsService());
			if(list.size() > 0){
				common.setAppFailMsg(msg , "部落名:"+ theForm.getName() +" 已经在使用，请更换新的部落名。");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			
			DbcpPool.beginTran(cn);

			//取得创建部落的积分
			JifenServiceImpl jifenservice = new JifenServiceImpl();
			
			//更新创建人积分
			jifenservice.compjf(cn, 9, theForm.getUserID(), msg);
			if(msg.getErrCode() != 0){
				return;
			}	
			
			theForm.setGroupID(SequenceMgr.generateSeq(GlbFinal.SEQ_INITDATE,GlbFinal.SEQ_GROUPID));

			//取得上传文件1
			if(theForm.getPicFile() != null&&!theForm.getPicFile().getFileName().equals("")){
				
				FilesForm fileForm = new FilesForm();
				fileForm.setFormFile(theForm.getPicFile());
				fileForm.setF_info(theForm.getName());
				fileForm.setF_Zip(GlbFinal.ZIP_135_110);
				
				FilesServiceImpl fileservice = new FilesServiceImpl();
				
				fileservice.add(cn, fileForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}
				theForm.setPic(fileForm.getF_ID());
			}		
			//取得FORM中传入的查询参数
			Object[] fields = new Object[23];

			fields[0] = theForm.getGroupID();			
			fields[1] = theForm.getVillageID();			
			fields[2] = theForm.getName();
			fields[3] = theForm.getSubject();
			fields[4] = theForm.getFirsort();
			fields[5] = theForm.getSecsort();
			fields[6] = theForm.getThiSort();
			fields[7] = theForm.getPic();
			fields[8] = theForm.getWork_location();
			fields[9] = theForm.getWork_sublocation();
			fields[10] = theForm.getWork_Area();
			fields[11] = 1;
			fields[12] = GlbFinal.GROUP_NUM;
			fields[13] = DatetimeUtil.getDatetime();
			fields[14] = theForm.getUserID();
			fields[15] = theForm.getPepoleVal();
			fields[16] = theForm.getPartakeVal();
			fields[17] = theForm.getContrVal();
			fields[18] = theForm.getType();
			fields[19] = 0;
			fields[20] = GlbFinal.GROUP_ALBUMSIZE;
			fields[21] = GlbFinal.STS_NORMAL;
			fields[22] = DatetimeUtil.getDatetime();
			
			sql = servSql.getInsertSql();
						
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , "新增群组失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}
			
			//在群成员表中增加创建人员
			Object[] fields6 = new Object[7];
			
			fields6[0] = theForm.getGroupID();
			fields6[1] = theForm.getUserID();
			fields6[2] = "";
			fields6[3] = GlbFinal.GROUPMEMBER_CREATOR;
			fields6[4] = DatetimeUtil.getDatetime();
			fields6[5] = GlbFinal.STS_NORMAL;
			fields6[6] = DatetimeUtil.getDatetime();
			
			GMemberServiceSqlMysql memberservSql = new GMemberServiceSqlMysql();

			sql = memberservSql.getInsertSql();
						
			res = bSqlServ.dbWrite(cn, sql, fields6);
			if(!res){
				common.setAppFailMsg(msg , " 加入群组成员失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}					
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			common.setAppFailMsg(msg , ex.getMessage());
			logger.error("System error , param get about operlog" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}


	/**
	 * 修改群组人数
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void updNum(Connection cn, GroupsForm theForm, ReturnMessage msg) 
	{

		try
		{
			//修改群成员数
			Object[] fields2 = new Object[1];
			
			fields2[0] = theForm.getGroupID();			
			
			GroupsServiceSqlMysql groupsservSql = new GroupsServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
	
			String sql = groupsservSql.getbykeySql();			
					
			List list = bSqlServ.dbRead(cn, sql, fields2, new DbGroupsService());
			if(list.size() ==0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			BaseGroups base = (BaseGroups)list.get(0);					
	
			//判断是否超过最大人数
			if(base.getJoinNum()+ theForm.getJoinNum() > base.getNum()){
				common.setAppFailMsg(msg , "已经达到最大人数");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}	
			
			//取得FORM中传入的查询参数
			Object[] fields3 = new Object[6];
			
			fields3[0] = base.getJoinNum() + theForm.getJoinNum();
			fields3[1] = base.getPepoleVal() + theForm.getPepoleVal();
			fields3[2] = base.getPartakeVal() + theForm.getPartakeVal();
			fields3[3] = base.getContrVal() + theForm.getContrVal();
			fields3[4] = base.getUseAlbumSize() + theForm.getUseAlbumSize();
			fields3[5] = base.getGroupID();
			
			sql = groupsservSql.getUpdNumSql();
						
			boolean  res = bSqlServ.dbWrite(cn, sql, fields3);
			if(!res){
				common.setAppFailMsg(msg , " 修改部落资料失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}	
			BeanUtils.copyProperties(theForm, base);
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about groups" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		}
		
		return;
	
	}	
	/**
	 * 删除一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void delete(GroupsForm theForm, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);

			//取得FORM中传入的查询参数
			Object[] fields = new Object[1];
			
			fields[0] = theForm.getGroupID();
			
			GroupsServiceSqlMysql servSql = new GroupsServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			String sql = servSql.getDeleteSql();
								
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(res){
				common.setAppFailMsg(msg , " 更新记录失败，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;
			}
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	
	/**
	 * 更新一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void update(GroupsForm theForm,String path, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			//判断群组名称是否已经使用
			List list = null;

			GroupsServiceSqlMysql servSql = new GroupsServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			//取得FORM中传入的查询参数
			Object[] fields1 = new Object[2];
			
			fields1[0] = theForm.getName();	
			fields1[1] = theForm.getVillageID();	
			
			String sql = servSql.getbyNameSql();			
					
			list = bSqlServ.dbRead(cn, sql, fields1, new DbGroupsService());
//			if(list.size() > 0){
//				BaseGroups base = (BaseGroups)list.get(0);
//				if(!base.getName().equals(theForm.getName())){
//					common.setAppFailMsg(msg , "群组名:"+ theForm.getName() +" 已经在使用，请更换新的群组名。");
//					DbcpPool.closeTran(cn);
//					DbcpPool.returnConnection(cn);
//					return;				
//				}
//			}			

			DbcpPool.beginTran(cn);
			
			//取得FORM中传入的查询参数
			Object[] fields2 = new Object[1];
			fields2[0] = theForm.getGroupID();			
			
			sql = servSql.getbykeySql();			
					
			list = bSqlServ.dbRead(cn, sql, fields2, new DbGroupsService());
			if(list.size() ==0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			BaseGroups base = (BaseGroups)list.get(0);			

			//取得上传文件1
			if(theForm.getPicFile() != null&&!theForm.getPicFile().getFileName().equals("")){
				
				FilesForm fileForm = new FilesForm();
				fileForm.setFormFile(theForm.getPicFile());
				fileForm.setF_info(theForm.getName());
				fileForm.setF_Zip(GlbFinal.ZIP_135_110);
				
				FilesServiceImpl fileservice = new FilesServiceImpl();
				
				fileservice.add(cn, fileForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}
				theForm.setPic(fileForm.getF_ID());
			}		
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[23];
			
			fields[0] = theForm.getVillageID();
			fields[1] = theForm.getName();
			fields[2] = theForm.getSubject();
			fields[3] = theForm.getFirsort();
			fields[4] = theForm.getSecsort();
			fields[5] = theForm.getThiSort();
			fields[6] = theForm.getPic();
			fields[7] = theForm.getWork_location();
			fields[8] = theForm.getWork_sublocation();
			fields[9] = theForm.getWork_Area();
			fields[10] = theForm.getJoinNum();
			fields[11] = theForm.getNum();
			fields[12] = theForm.getCtime();
			fields[13] = theForm.getUserID();
			fields[14] = theForm.getPepoleVal();
			fields[15] = theForm.getPartakeVal();
			fields[16] = theForm.getContrVal();
			fields[17] = theForm.getType();
			fields[18] = theForm.getUseAlbumSize();
			fields[19] = theForm.getAlbumSize();
			fields[20] = theForm.getStatus();
			fields[21] = DatetimeUtil.getDatetime();
			fields[22] = theForm.getGroupID();
			
			sql = servSql.getUpdateSql();
						
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 修改群组信息失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			common.setAppFailMsg(msg , ex.getMessage());
			logger.error("System error , param get about operlog" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	/**
	 *发送激活邮件
	 * @see net.IUserbasicService.module.other.IUserService#add(net.swift.common.ReturnMessage)
	 */
	public void sendEmail(GroupsForm theForm,String path, ReturnMessage msg) 
	{

		//发送邮件到注册邮箱
		Email themail = new Email(GlbFinal.EMAIL_SMTP);  
		StringBuffer emailcont = new StringBuffer("");

		themail.setNeedAuth(true);  
	    themail.setSubject(JstlFun.GetUserNick(theForm.getUserID()) + "邀请您加入群组：" + theForm.getName());  //题目
	    themail.setFrom(GlbFinal.EMAIL_ADDR,GlbFinal.EMAIL_FRNAME);//发件人
	    themail.setNamePass(GlbFinal.EMAIL_FRUSER,GlbFinal.EMAIL_FRUSERPSD);//用户、密码  

	    emailcont.delete(0, emailcont.length());
		emailcont.append("你好:<br>");
		emailcont.append(theForm.getCont());
	    themail.setBody(emailcont.toString());  //发送内容
		//调用email类，发送邮件
	    themail.setTo(theForm.getEmail());  //收件人
	    themail.sendout();  			
		
	}

}
